package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.QueryConditionVo;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.common.utils.StringUtils;
import com.xhh.nphm.system.domain.MwfSendBack;
import com.xhh.nphm.system.domain.TwfSendBackEty;
import com.xhh.nphm.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/mMailWF/rCity")
public class MMailWFrCityController extends BaseController {
    private String prefix = "system/mMailWF/rCity";
    @Autowired
    private MwfSendBackService wfSendBackService;

    @Autowired
    private ISmsNoteService smsNoteService;

    /**
     * 进入待分发下级列表页面
     */
    @GetMapping("/sendWaitList")
    public ModelAndView sendWaitList() {
        return new ModelAndView(prefix + "/sendWaitList");
    }

    /**
     * 加载待分发下级列表数据
     */
    @PostMapping("/sendWaitListJson")
    public TableDataInfo sendWaitListJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list;
        if(IS_GAOXIN()){
            list = wfSendBackService.sendWaitListJson_gxq(user.getDeptId(), workFormNo, sdeptName);
        }else{
            list = wfSendBackService.sendWaitListJson(user.getDeptId(), workFormNo, sdeptName);
        }
        return getDataTable(list);
    }

    /**
     * 进入下级退回M列表页面
     */
    @GetMapping("/listSonBack")
    public ModelAndView listSonBack() {
        return new ModelAndView(prefix + "/listSonBack");
    }

    /**
     * 加载下级退回M列表数据
     */
    @PostMapping("/listSonBackJson")
    public TableDataInfo listSonBackJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = wfSendBackService.listSonBackJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }

    /**
     * 进入下级退回已处理M列表页面
     */
    @GetMapping("/listSonABack")
    public ModelAndView listSonABack() {
        return new ModelAndView(prefix + "/listSonABack");
    }



    /**
     * 进入下级退回全退工单M列表页面
     */
    @GetMapping("/listAllSonABack")
    public ModelAndView listAllSonABack() {
        return new ModelAndView(prefix + "/listAllSonABack");
    }

    /**
     * 加载下级退回M列表数据
     */
    @PostMapping("/listAllSonABackJson")
    public TableDataInfo listAllSonABackJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = wfSendBackService.listAllSonABackJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }


    /**
     * 进入下级退回M列表页面
     */
    @GetMapping("/listSonNABack")
    public ModelAndView listSonNABack(ModelMap map) {
        SysUser user = getSysUser();
        map.put("smsInform",smsNoteService.selectSmsInformMail(user.getDeptId().toString(),user.getUserId().toString()));
        return new ModelAndView(prefix + "/listSonNABack");
    }

    /**
     * 加载下级退回M列表数据
     */
    @PostMapping("/listSonNABackJson")
    public TableDataInfo listSonNABackJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName, @RequestParam("spareString") String spareString) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.listSonNABackJson_gxq(user.getDeptId(), workFormNo, sdeptName,spareString);
        }else{
            list = wfSendBackService.listSonNABackJson(user.getDeptId(), workFormNo, sdeptName,spareString);
        }
        return getDataTable(list);
    }



    /**
     * 进入下级已退工单M列表页面
     */
    @GetMapping("/listSonAgreeBack")
    public ModelAndView listSonAgreeBack() {
        return new ModelAndView(prefix + "/listSonAgreeBack");
    }

    /**
     * 加载下级已退M列表数据
     */
    @PostMapping("/listSonAgreeBackJson")
    public TableDataInfo listSonAgreeBackJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName, @RequestParam("spareString") String spareString) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = wfSendBackService.listSonAgreeBackJson(user.getDeptId(), workFormNo, sdeptName,spareString);
        return getDataTable(list);
    }

    /**
     * 进入下级未签收M列表页面
     * @return 视图
     */
    @GetMapping("/listSonSignNot")
    public ModelAndView listSonSignNot() {
        return new ModelAndView(prefix + "/listSonSignNot");
    }

    /**
     * 加载下级未签收M列表页面
     * @param workFormNo 工单编号
     * @param sdeptName 派发单位
     * @return 列表数据
     */
    @PostMapping("/listSonSignNotJson")
    public TableDataInfo listSonSignNotJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = wfSendBackService.listSonSignNotJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }


    /**
     * 进入下级已办毕工单 待处理列表页面
     *
     * @return 视图
     */
    @GetMapping("/listSonOverUntreated")
    public ModelAndView listSonOverUntreated() {
        return new ModelAndView(prefix + "/listSonOverUntreated");
    }
    /**
     * 进入下级已办毕工单 已处理列表页面
     *
     * @return 视图
     */
    @GetMapping("/listSonOverTreated")
    public ModelAndView listSonOverTreated() {
        return new ModelAndView(prefix + "/listSonOverTreated");
    }

    /**
     * 加载下级已办毕工单 待处理列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/listSonOverUntreatedJson")
    public TableDataInfo listSonOverUntreatedJson(MwfSendBack sendBackEty) {
        startPage();
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.listSonOverUntreatedJson_gxq(sendBackEty,getSysUser().getDeptId());
        }else{
            list = wfSendBackService.listSonOverUntreatedJson(sendBackEty,getSysUser().getDeptId());
        }
        return getDataTable(list);
    }

    /**
     * 加载下级已办毕工单 已处理列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/listSonOverTreatedJson")
    public TableDataInfo listSonOverTreatedJson(MwfSendBack sendBackEty) {
        startPage();
        List<Map<String, Object>> list = wfSendBackService.listSonOverTreatedJson(sendBackEty,getSysUser().getDeptId());
        return getDataTable(list);
    }

    /**
     * 进入下级办理中工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/listSonWait")
    public ModelAndView listSonWait() {
        return new ModelAndView(prefix + "/listSonWait");
    }


    /**
     * 加载下级办理中工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/listSonWaitJson")
    public TableDataInfo listSonWaitJson(QueryConditionVo qc) {
        startPage();
        SysUser user = getSysUser();
        qc.setUserDeptId(String.valueOf(user.getDeptId()));
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.listSonWaitJson_gxq(qc);
        }else{
            list = wfSendBackService.listSonWaitJson(qc);
        }
        return getDataTable(list);
    }

    /**
     * 催办工单
     * @param ids
     * @return
     */
    @PostMapping("/urgeTransact")
    public AjaxResult urgeTransact(String ids, String wfNo, String dept, int smsInform){
        if(StringUtils.isNotNull(ids)&&StringUtils.isNotNull(wfNo)&&StringUtils.isNotNull(dept)){
            if(ids.contains(",")){
                String[] id = ids.split(",");
                for (int i = 0; i < id.length; i++) {
                    wfSendBackService.updateUrgeTransact(id[i]);
                }
            }else {
                wfSendBackService.updateUrgeTransact(ids);
            }
            if(smsInform == 1){
                smsNoteService.sendSmsNote(dept,wfNo,"催办");
            }
            return success();
        }
        return error("催办失败，请重试");
    }

    /**
     * 催办工单
     * @param ids
     * @return
     */
    @PostMapping("/supUrgeTransact")
    public AjaxResult supUrgeTransact(String ids, String wfIds, String wfNos, int smsInform){
        if(StringUtils.isNotNull(ids)&&StringUtils.isNotNull(wfIds)&&StringUtils.isNotNull(wfNos)){
            StringBuilder deptIds = new StringBuilder();
            StringBuilder workFormNo = new StringBuilder();
            List<MwfSendBack> listSendDept = new ArrayList<>();
            if(ids.contains(",")){
                String[] pkId = ids.split(",");   // 派单
                String[] wfId = wfIds.split(","); // 工单
                String[] wfNo = wfNos.split(","); // 工单编号
                for (int i = 0; i < pkId.length; i++) {
                    listSendDept = wfSendBackService.listSendDeptSon(Integer.parseInt(wfId[i]), getSysUser().getDeptId());
                    for (MwfSendBack sendBack : listSendDept) {
                        if(sendBack.getBackState() == 0 || sendBack.getBackState() == 10){
                            deptIds.append(sendBack.getSbdeptId()).append(",");
                            workFormNo.append(wfNo[i]).append(",");
                            wfSendBackService.updateUrgeTransact(String.valueOf(sendBack.getPkId()));
                        }
                    }
                }
            }else {
                listSendDept = wfSendBackService.listSendDeptSon(Integer.parseInt(wfIds), getSysUser().getDeptId());
                for (MwfSendBack sendBack : listSendDept) {
                    if(sendBack.getBackState() == 0 || sendBack.getBackState() == 10){
                        deptIds.append(sendBack.getSbdeptId()).append(",");
                        workFormNo.append(wfIds).append(",");
                        wfSendBackService.updateUrgeTransact(String.valueOf(sendBack.getPkId()));
                    }
                }
            }
            if(smsInform == 1){
                smsNoteService.sendSmsNote(deptIds.toString(),workFormNo.toString(),"催办");
            }
            return success();
        }
        return error("催办失败，请重试");
    }
}
